import { IQuery, IResponse, ITableResponse } from "@/interface/IAxios";
import { IShapeProps } from "@/interface/IShape";
import { IStage } from "@/interface/IStage";
import http from "../../config/http";
import { ITemplate, TemplateStatistics } from "./types";

// 查询模板管理列表
export function listTemplate(query?: IQuery): Promise<ITableResponse<ITemplate>> {
  return http.request({
    url: "/template/template/list",
    method: "get",
    params: query
  });
}

// 查询模板管理列表
export function listMarket(query?: IQuery) {
  return http.request({
    url: "/template/template/market",
    method: "get",
    params: query
  });
}
// 查询模板管理列表
export function previewTpl(data: any) {
  return http.request({
    url: "/template/template/previewTpl",
    method: "post",
    data
  });
}

export function addTpl(data: { stage: IStage; templateList: Array<IShapeProps> }) {
  return http.request({
    url: "/template/template/addTpl",
    method: "post",
    data
  });
}

// 查询模板管理详细
export function getTemplate(id: string): Promise<IResponse<ITemplate>> {
  return http.request({
    url: `/template/template/${id}`,
    method: "get"
  });
}
// 新增模板管理
export function saveTpl(
  data: {
    stage: IStage;
    lcd: boolean;
  },
  /* 是否立即下发 */
  immediately = false
) {
  return http.request({
    url: `/template/template/saveTpl?immediately=${immediately}`,
    method: "post",
    data
  });
}

// 修改模板管理
export function updateTemplate(data: ITemplate): Promise<IResponse<any>> {
  return http.request({
    url: "/template/template",
    method: "put",
    data
  });
}

// 删除模板管理
export function delTemplate(id: Array<string>): Promise<IResponse<any>> {
  return http.request({
    url: `/template/template/${id}`,
    method: "delete"
  });
}
type CopyTemplate = {
  copyList: string[];
  rename: string;
};
export function copyTemplate(data: CopyTemplate): Promise<IResponse<any>> {
  return http.request({
    url: `/template/template/copy`,
    method: "post",
    data
  });
}

export function refOptions(id: string): Promise<IResponse<TemplateRefs[]>> {
  return http.request({
    url: `/template/template/refOptions`,
    method: "get",
    params: {
      scope: 3,
      templateId: id
    }
  });
}

// 获取绑定的模板的数量
export function getBoundCount(id: string): Promise<IResponse<any>> {
  return http.request({
    url: "/template/template/getBoundCount",
    method: "get",
    params: {
      templateId: id
    }
  });
}

export const templateQuery = (queryType?: 0): Promise<IResponse<TemplateStatistics[]>> => {
  return http.request({
    url: "template/template/query",
    method: "get",
    params: {
      queryType
    }
  });
};
